﻿using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

[CustomEditor(typeof(Scenery))]
public class SceneryEditor : BaseEditor
{
	private bool addArea = false;
	private bool addInside = false;

	private Vector3 currentPos;
	private Scenery scenery;

	private void OnEnable()
	{
		scenery = target as Scenery;
	}

	public override void OnInspectorGUI ()
	{
		base.OnInspectorGUI ();

		GUILayout.Space(10);

		EditorGUILayout.BeginHorizontal();
		{
			GUI.enabled = !addArea;
			if (GUILayout.Button("Edit Outside Tiles", GUILayout.Height(40)))
			{
				addArea = !addArea;
				addInside = false;
			}
			
			GUI.enabled = !addInside;
			if (GUILayout.Button("Edit Inside Tiles", GUILayout.Height(40)))
			{
				addInside = !addInside;
				addArea = false;
			}

			GUI.enabled = true;
		}
		EditorGUILayout.EndHorizontal();

		Instructions();

		if (GUI.changed)
		{
			EditorUtility.SetDirty(scenery);
		}
	}

	protected virtual void OnSceneGUI()
	{
		List<Vector3> list = null;
		if (addArea)
		{
			list = scenery.outsidePositions;
		}
		else if (addInside)
		{
			list = scenery.insidePositions;
		}
		else
		{
			return;
		}

		UpdateSceneTile();
		/*foreach (Vector3 pos in list)
		{
			bool drawRed = sceneTile.type == SceneTile.Type.Remove
				&& v == sceneTile.tile.transform.position - scenery.transform.position;
			DrawSquare(v + scenery.transform.position, drawRed ? Color.red : Color.green);
			DrawSquare(scenery.PosToWorld(pos), Color.green);
		}*/

		/*if (sceneTile.type == SceneTile.Type.Add && sceneTile.tile != null)
		{
			DrawSquare(sceneTile.tile.transform.position, Color.yellow);
		}*/
		
		
		if (sceneTile.tile != null && sceneTile.click)
		{
			Debug.Log(scenery.WorldToPos(sceneTile.tile.transform.position));
			Vector3 pos = scenery.WorldToPos(sceneTile.tile.transform.position);//sceneTile.tile.transform.position - scenery.transform.position;
			if (sceneTile.type == SceneTile.Type.Remove)
			{
				list.Remove(pos);
			}
			else if (sceneTile.type == SceneTile.Type.Add && !list.Contains(pos))
			{
				list.Add(pos);
			}

			GUI.changed = true;
		}

		if (GUI.changed)
		{
			EditorUtility.SetDirty(scenery);
		}
	}
}

[CustomEditor(typeof(SceneChanger))]
public class SceneChangeEditor : SceneryEditor
{
	public override void OnInspectorGUI ()
	{
		base.OnInspectorGUI ();
	}
	
	protected override void OnSceneGUI ()
	{
		base.OnSceneGUI ();
	}
}

[CustomEditor(typeof(FloorNumber))]
public class FloorNumberEditor : SceneryEditor
{
	public override void OnInspectorGUI ()
	{
		base.OnInspectorGUI ();
	}
	
	protected override void OnSceneGUI ()
	{
		base.OnSceneGUI ();
	}
}
